﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maths
{
    class Program
    {
        static MathsClass m = new MathsClass();
        static void Main(string[] args)
        {
            m.MultiplesOf3and5();
            m.EvenFibonacci();
            m.PrimeNumber();
            m.PalinDrome();
            //m.SmallestMultiple();
            m.SumSquareDifference();
            m.LargestProductInASequence();
            m.PytahgTriplets();
           // m.HighlyDivisibleTriangular();
            m.PowerDigitSum();
            Console.ReadKey();
        }


    }
    class MathsClass
    {
        public void MultiplesOf3and5()
        {
            int sum = 0;
            int count = 0;
            for (int i = 1; i < 1000; i++)
            {
                count++;
                if (i % 3 == 0 || i % 5 == 0)
                {

                    sum += i;
                }
            }
            Console.WriteLine("Total values counted : {0} \nTotal sum of values : {1}", count, sum);
        }
        public void EvenFibonacci()
        {
            int sum = 0;
            int last = 0;
            int current = 1;
            for (int i = 0; i < 4000000; i++)
            {
                if (i == last + current)
                {
                    last = current;
                    current = i;
                    if (i % 2 == 0)
                    {
                        sum += current;
                    }
                }
            }
            Console.WriteLine("Sum : {0}", sum);
        }
        public void PrimeNumber()
        {
            ulong largest_pFact;
            uint i = 2;
            largest_pFact = 600851475143;


            while (i < Math.Sqrt((double)largest_pFact))
            {
                if (i % 2 != 0 | i == 2)
                {
                    if (largest_pFact % i == 0)
                        largest_pFact /= i;
                }

                i++;
            }

            Console.WriteLine(largest_pFact);
        }
        public void PalinDrome()
        {
            int answer = 0;
            for (int i = 10; i < 1000; i++)
            {
                for (int j = 10; j < 1000; j++)
                {
                    if (i * j > answer)
                    {
                        int temp = i * j;
                        string test = temp.ToString();
                        char[] c = test.ToCharArray();
                        char[] reverse = new char[c.Length];
                        for (int x = 0; x < c.Length; x++)
                        {
                            reverse[c.Length - 1 - x] = c[x];
                        }
                        string output = "";
                        for (int builder = 0; builder < reverse.Length; builder++)
                        {
                            output += reverse[builder];
                        }
                        if (test == output)
                        {
                            answer = int.Parse(test);
                        }
                    }
                }
            }
            Console.WriteLine("The Biggest Palindrome is {0}", answer);
        }
        public void SmallestMultiple()
        {
            int numberOfMultiples = 20;
            int SmallestNumber = 0;
            for (int i = 0; i < 100000000000; i++)
            {
                int count = 0;
                for (int j = 1; j <= numberOfMultiples; j++)
                {
                    if (MultipleOf(i, j))
                        count++;
                }
                if (count == numberOfMultiples)
                { SmallestNumber = i; break; }
            }
            Console.WriteLine(SmallestNumber);
        }
        public void SumSquareDifference()
        {
            int numOfNaturalNumbers = 100;
            int sumOfSquares = 0;
            int squareOfSum = 0;
            for (int i = 1; i <= numOfNaturalNumbers; i++)
            {
                sumOfSquares += (i * i);
            }
            for (int i = 0; i <= numOfNaturalNumbers; i++)
            {
                squareOfSum += i;
            }
            squareOfSum = (squareOfSum * squareOfSum);
            Console.WriteLine("Sum square Difference {0}", squareOfSum - sumOfSquares);
        }
        private bool MultipleOf(int number, int divider)
        {
            if (number % divider == 0 && number != 0)
                return true;
            else
                return false;

        }
        public void LargestProductInASequence()
        {
            #region Sequence String
            const string sequene = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
            #endregion

            char[] charSequence = sequene.ToCharArray();
            int HighestProduct = 0;
            for (int i = 0; i < charSequence.Length - 5; i++)
            {
                int temp = 1;
                for (int j = 0; j < 5; j++)
                {
                    temp *= int.Parse(charSequence[i + j].ToString());
                    
                }
                if (temp > HighestProduct)
                    HighestProduct = temp;
            }
            Console.WriteLine(HighestProduct);

        }

        public void PytahgTriplets()
        {
            for (int i = 0; i < 200; i++)
            {
                int result = (3 * i) + (4 * i) + (5 * i);
                if ( result == 1000)
                    Console.WriteLine(i);
            }
        }

        public void HighlyDivisibleTriangular()
        {
            int sum = 0;
            int diviors = 500;
            int highestCountSoFar = 0;
            for (int i = 1; i < 10000000; i++)
            {
                int count = 0;
                sum += i;
                for (int j = 1; j <= sum; j++)
                {
                    if (sum % j == 0)
                        count++;
                }
                if (count > diviors)
                { Console.WriteLine("The {0}th term ({1}) is the first triangle number to have over {2} divisors", i, sum, diviors); break; }
                else { if (count > highestCountSoFar) { highestCountSoFar = count; } }
            }
            
        }

        public void PowerDigitSum()
        {
            int powerOf = 64;
            ulong sum = 2;
            for (int i = 1; i < powerOf; i++)
            {
                sum *= 2;
            }
            char[] digits = sum.ToString().ToCharArray();
            int sumOfDigits = 0;
            for (int i = 0; i < digits.Length; i++)
            {
                sumOfDigits += int.Parse(digits[i].ToString());
            }
            Console.WriteLine(sumOfDigits);
        }
    }
}
